/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.servlet.book.content;

import java.util.Collection;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import offset.nodes.Constants;
import offset.nodes.server.servlet.book.NameBuilder;
import offset.nodes.server.servlet.book.provider.NodeProvider;

/**
 *
 * @author Walter L�tgenau
 */
public class CssResource {

    private HashMap<String, Resource> resources = new HashMap<String, Resource>();
    private NodeProvider nodeProvider;
    boolean scriptingEnabled;
    String content;

    public CssResource(String repositoryRef, String content, NameBuilder nameBuilder) {
        init(repositoryRef, content, nameBuilder);
    }

    /**
     * Parse the CSS content for images. These shall be added as recources,
     * for later download and addition to the book.
     * 
     * @param repositoryRef
     * @param content
     * @param nameBuilder
     * @throws Exception
     */
    public void init(String repositoryRef, String content, NameBuilder nameBuilder) {
        String path;
        if (repositoryRef.lastIndexOf(Constants.PATH_SEPARATOR) >= 0)
            path = repositoryRef.substring(0, repositoryRef.lastIndexOf(Constants.PATH_SEPARATOR));
        else
            path = "";

        Pattern pattern = Pattern.compile("url\\((.*)\\)");
        Matcher matcher = pattern.matcher(content);

        StringBuffer result = new StringBuffer();
        while (matcher.find()) {
            String name = matcher.group(1);

            name = stripContextAndServlet(name);
            
            Resource resource;
            if (name.startsWith(Constants.PATH_SEPARATOR))
                resource = new Resource(name);
            else
                resource = new Resource(path + Constants.PATH_SEPARATOR + name);
            
            resource.setBookRef(nameBuilder.buildImageName(resource.getRepositoryRef()));
            
            addResource(resource);

            matcher.appendReplacement(result, "url(" + nameBuilder.buildCssImageName(resource.getRepositoryRef()) + ")");
        }
        matcher.appendTail(result);

        setContent(result.toString());
    }

    protected String stripContextAndServlet(String name) {
        if (!name.startsWith(Constants.PATH_SEPARATOR + Constants.CONTEXTMAPPING_NODES))
            return name;

        int start = name.indexOf(Constants.PATH_SEPARATOR, Constants.CONTEXTMAPPING_NODES.length() + 2);
        return name.substring(start + 1);
    }

    public Collection<Resource> getResources() {
        return resources.values();
    }

    public void addResource(Resource resource) {
        resource.setContent(new BinaryContent());
        resources.put(resource.getRepositoryRef(), resource);
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}
